/*
 * Sesame Inc.
 * Copyright (c) 2009 All Rights Reserved.
 */
package com.sesame.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.sesame.domain.Album;
import com.sesame.domain.User;
import com.sesame.service.AlbumService;
import com.sesame.service.UserService;

/**
 * @author voidman
 * 
 */
public class EditAlbumController extends SimpleFormController {

    private AlbumService albumService;
    private Integer albumId = null;

    public void setAlbumService(AlbumService albumService) {
        this.albumService = albumService;
    }

    protected Object formBackingObject(HttpServletRequest request)
            throws Exception {
        albumId = Integer.parseInt(request.getParameter("albumId"));
        Album album = albumService.showAlbum(albumId);
        return album;
    }

    protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {

        Album album = (Album) command;
        album.setId(albumId);

        Map model = errors.getModel();

        if (albumService.isEditAlbum(album, model)) {
            return new ModelAndView("redirect:home.html");
        } else {
            return new ModelAndView(getFormView(), model);
        }

    }

}